<link href="admin.css" rel="stylesheet" type="text/css" />
<script src="<{$config.weburl}>/script/jquery-1.4.4.min.js" type="text/javascript" charset="utf-8" ></script>
<script type="text/javascript">
	$(function(){
		/* 全选 */
		 $('.checkall').click(function(){
			var _self = this;
			$('.checkitem').each(function(){
				if (!this.disabled)
				{
					$(this).attr('checked', _self.checked);
				}
			});
			$('.checkall').attr('checked', this.checked);
		});	 
	});
</script>
<div class="container dialog" style="width:94%; margin:auto;">
<form action="" method="get">
<input type="hidden" name="m" value="<{$smarty.get.m}>">
<input type="hidden" name="s" value="<{$smarty.get.s}>">
<input type="hidden" name="type" value="<{$smarty.get.type}>">
<input type="hidden" id="spec" value="<{$smarty.get.spec|default:0}>">

<table class="table select_table" style="margin-bottom:0;">
    <tbody>
    	<tr>
            <td width="70">产品名称:</td>
            <td width="260"><input type="text" name="key" class="w250" value="<{$smarty.get.key}>" /></td>
			<td width="70">产品分类:</td>
            <td>
            <select name="catid" id="classes">
            	<option value="0">选择分类</option>
            </select>
            </td>
        </tr>
        <tr>    
            <td width="70">序号:</td>
            <td width="250"><input type="text" name="id" class="w250" value="<{$smarty.get.id}>" /></td>
            <td><input type="image" src="<{$config.weburl}>/image/admin/search.gif" /></td>
            <td>注：多个产品使用,隔开</td>
        </tr>
    </tbody>
</table>
</form>
<form action="" method="post">
<table class="table alc" id="<{$smarty.get.type}>_table">
    <tbody>
        <tr class="header">
            <th width="20"></th>
             <th width="10">数量</th>
            <th width="50">序号</th>
            <th width="100">库存</th>
            <th class="al">产品名称</th>
            <th width="50">规格</th>
            <th width="50">单位</th>
            <th width="80">销售单价</th>
            <th width="80">最小单位</th>
            <th width="80">最多单位</th>
            
        </tr>
        <{foreach item=list key=num from=$de.list}>
        <tr>
            <td><input type="checkbox" value="<{$list.id}>" class="checkitem" name="chk[]" id="chk_<{$num}>"></td>
             <td><input type="text" value="1" maxnum="<{$list.maxnum+0}>" minnum="<{$list.minnum+0}>"  stocks="<{if $list.stocks-$list.canbuyout>0}><{$list.stocks+0-$list.canbuyout}><{else}>0<{/if}>" id="num_<{$num}>" size='5'></td>
             <td class="al"> <input type="hidden" value="<{$list.id}>" name="related_id[]"><{$list.id}></td>
             <td><{if $list.stocks-$list.canbuyout>0}><{$list.stocks+0-$list.canbuyout}> <{$list.unit}>(含虚拟<{$list.stocks+0-$list.canbuyout-$list.stocks}>)<{else}><font color="red">0</font><{/if}>&nbsp;</td>
            <td class="al">
            <div class="name" title="<{$list.pname}>"><{$list.title}></div>
            </td>
           <td><{$list.spec}></td>
           <td><{$list.unit}></td>
            <td><{$list.pricesell}><input type="hidden" value="<{$list.pricesell}>" id="pricesell_<{$num}>"></td>
            <td><{$list.minnum}><input type="hidden" value="<{$list.pricebuy}>" id="pricebuy_<{$num}>"></td>
            <td><{$list.maxnum}><input type="hidden" value="<{$list.priceundersell}>" id="priceundersell_<{$num}>"></td>
        </tr>
        <{foreachelse}>
        <tr>
            <td class="norecord" colspan="99"><i></i><span>暂无符合条件的数据记录</span></td>
        </tr>
        <{/foreach}>
    </tbody>
    <tfoot>
        <tr>
            <td colspan="99">
            	<div class="fl">
                <input type="checkbox" class="checkall" id="del">
                <input type="button" id="save" value="保存" />
                </div>
                <div class="page"><{$de.page}></div>
            </td>
        </tr>
    </tfoot>
</table>
</form>
<script>
$.getJSON('<{$config.weburl}>/admin/module.php?m=inventory&s=classes.php&uri=api',function(data){
	//alert(data);
	var catid=<{$smarty.get.catid}>
	$.each(data,function(key,item){
		$('#classes').append('<option value="'+item.id+'" '+(item.id==catid?'selected':'')+' >'+item.title+'</option>');
		if(item.children){
			$.each(item.children,function(k,v){
				$('#classes').append('<option value="'+v.id+'" '+(v.id==catid?'selected':'')+'>|——'+v.title+'</option>');

			})
		}
	})
});

$('input[maxnum]').blur(function(){
	
	var maxnum=parseInt($(this).attr('maxnum'));
	var minnum=parseInt($(this).attr('minnum'));

	var value=parseInt($(this).val());
	if(value<=0||isNaN(value)){
		$(this).val(0);
	}
	if(value>maxnum){
		$(this).val(maxnum);
	}else if(value<minnum){
		$(this).val(minnum);
	}else{
		$(this).val(value);
	}

});
function setToParent(){
	var obj=$("input[class='checkitem']:checked");
	if(obj.length)
	{
		
		
		if(parent.$data==undefined){
			parent.$data={};
			
			}
			if(parent.$data.inventory==undefined){
				parent.$data["inventory"]={};
				}
			
			if($('#spec').val()!=0){
				parent.$data[$('#spec').val()]={};
			}
		$.each(obj,function(key,item){
			var id=$(this).attr('id');
			var num=id.split('_')[1];
			
 			var nums=$('#num_'+num).val();
			var priceundersell=$('#priceundersell_'+num).val();
			var pricebuy=$('#pricebuy_'+num).val();
			var pricesell=$('#pricesell_'+num).val();
			var stocks=$('#num_'+num).attr('stocks');
 			
			if($('#spec').val()!=0){
				parent.$data[$('#spec').val()]["d_"+$(this).val()]={id:$(this).val(),nums:nums,priceundersell:priceundersell,pricebuy:pricebuy,pricesell:pricesell,stocks:stocks};
			}else{
				parent.$data.inventory["d_"+$(this).val()]={id:$(this).val(),nums:nums,priceundersell:priceundersell,pricebuy:pricebuy,pricesell:pricesell,stocks:stocks};
			}
		})
		
		window.parent.main.setinventory($('#spec').val());
	

		
	}
	

	
}


$('#save').click(function(){
	var obj=$("input[class='checkitem']:checked");
	if(!obj.length)
	{
		alert("请选择数据，再进行操作!");
		return false;
	}
	//window.parent.main.addrow("<{$smarty.get.type}>",0);
	setToParent();
	window.parent.DialogManager.close("<{$smarty.get.type}>");
});
</script>
</div>